package com.example.flowable.wsl;

import com.example.FlowableDb;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * Flowable通用服务测试类
 *
 * @author wsl
 */
public class FlowableGeneralServiceImpl extends FlowableDb {

    public FlowableGeneralServiceImpl() {
        super("");
    }

    /**
     * 查询我要处理的所有任务
     */
    @Test
    public void myTasks() {
        //1、查询历史任务信息（包括完成与未完成的）
        List<HistoricTaskInstance> list = historyService
                .createHistoricTaskInstanceQuery()
                .taskAssignee("11")
                .list();

        for (HistoricTaskInstance historicTaskInstance : list) {
            //2、查询流程定义信息
            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                    .processDefinitionId(historicTaskInstance.getProcessDefinitionId())
                    .singleResult();
            System.out.println("任务名称：" + historicTaskInstance.getName() + "" +
                    "\n" + "任务编号：" + historicTaskInstance.getId() +
                    "\n" + "流程实例编号：" + historicTaskInstance.getProcessInstanceId() +
                    "\n" + "任务创建时间：" + historicTaskInstance.getCreateTime() +
                    "\n" + "任务是否已结束" + historicTaskInstance.getEndTime() +
                    "\n" + "流程定义名称：" + processDefinition.getName()
            );
        }
    }

    /**
     * 查询我发起的所有流程
     */
    @Test
    public void MyStarts(){
        //1、查询我发起的所有流程，按照发起时间倒序排列
        List<HistoricProcessInstance> list = historyService
                .createHistoricProcessInstanceQuery()
                .startedBy("11")
                .orderByProcessInstanceStartTime()
                .desc()
                .list();

        list.forEach(e->{
            System.out.println("发起时间：" + e.getStartTime() +
                    "\n流程名称" + e.getProcessDefinitionName() +
                    "\n是否结束" + e.getEndTime() +
                    "\n流程实例编号" + e.getId() +
                    "\n结束时间" + e.getEndTime()
            );
        });
    }
}
